<?php

namespace app\http\middleware;

use other\Token;
use think\facade\Request;
use think\facade\Validate;
use think\exception\ValidateException;
use think\Request as OriginRequest;
use app\api\service\user\UserNumber;

class CheckToken
{
    public function handle(OriginRequest $request, \Closure $next)
    {
        $data['token'] = Request::header('token');
        $rules = [
            'token' => 'require'
        ];
        $v = Validate::make($rules, []);
        $result = $v->check($data);
        if ( ! $result ) {
            throw new ValidateException('token验证失败');
        }
        $info = Token::decode($data['token']);
        $request->userInfo = (array)$info['userInfo'];
        if ( (new UserNumber())->status($request->userInfo['id']) == 1 ) {
            throw new ValidateException('您的账号已被禁用,请联系店家');
        }
        return $next($request);
    }
}